package com.way.designpatterncodepratice.prototype;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.boot.jackson.JsonObjectSerializer;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * @author liangliwei 80551025
 * @description
 * @date 2020-06-22
 */
public class Main {
    public static void main(String[] args) throws CloneNotSupportedException, IOException {
        Sheep sheep = new Sheep("doly",11,"white","china");
        Sheep friend = new Sheep("jacky",11,"white","china");
        sheep.setFriend(friend);


        /**
         * 传统做法
         */
        Sheep sheep1 = new Sheep();
        sheep1.setAddress(sheep.getAddress());
        sheep1.setAge(sheep.getAge());
        sheep1.setName(sheep.getName());
        sheep1.setColor(sheep.getColor());
        System.out.println(sheep.hashCode());
        System.out.println(sheep);
        System.out.println(sheep.getFriend());
        System.out.println("sheep friend hashCode:"+sheep.getFriend().hashCode());
        System.out.println(sheep1.hashCode());
        System.out.println(sheep1);
        /**
         * null 引用类型并没有复制
         */
        System.out.println(sheep1.getFriend());
        /**Exception in thread "main" java.lang.NullPointerException
         at com.way.designpatterncodepratice.prototype.Main.main(Main.java:30)
         *
         */
//        System.out.println(sheep1.getFriend().hashCode());
        System.out.println(sheep==sheep1);

        /**
         * 浅拷贝 是真的复制的话 hashcode都不一样 相当于真的多了一个
         */

        System.out.println("===浅拷贝====");
        Sheep sheep2 = (Sheep) sheep.clone();
        System.out.println(sheep2);
        System.out.println(sheep2.getFriend());
        /**
         * 这里观察到 sheep2的hashCode 跟 sheep的hashCode是一样的
         * 说明并没有真的复制了一个 只是引用地址一样 指向了同一个内存地址
         */
        System.out.println("friend hashCode:"+sheep2.getFriend().hashCode());
        System.out.println(sheep2.hashCode());
        System.out.println(sheep==sheep2);
        /**
         * true 浅拷贝 引用类型friend都是指向同一个
         */
        System.out.println(sheep.getFriend()==sheep2.getFriend());


        /**
         * 深拷贝 1clone 缺点：每个引用类型 都要复制
         */

        System.out.println("====深拷贝 1.重写clone() 主要是引用类型 =======");

        SheepDeepClone sheepDeepClone = new SheepDeepClone("doly",11,"white","china");
        SheepDeepClone friendDeepClone = new SheepDeepClone("jacky",11,"white","china");
        sheepDeepClone.setFriend(friendDeepClone);

        SheepDeepClone sheepDeepClone1 = (SheepDeepClone) sheepDeepClone.clone();
        System.out.println(sheepDeepClone);
        System.out.println(sheepDeepClone1);
        System.out.println(sheepDeepClone1==sheepDeepClone);
        System.out.println(sheepDeepClone1.getFriend()==sheepDeepClone.getFriend());


        System.out.println("====深拷贝 1.序列化 反序列化 io流 ObjectInputStream ObjectOutputStream  或者 json =======");
        ObjectMapper mapper = new ObjectMapper();
        String json = mapper.writeValueAsString(sheepDeepClone);
        SheepDeepClone sheepDeepClone2 = mapper.readValue(json, SheepDeepClone.class);
        System.out.println(sheepDeepClone);
        System.out.println(sheepDeepClone2);
        System.out.println(sheepDeepClone.hashCode());
        System.out.println(sheepDeepClone2.hashCode());
        System.out.println(sheepDeepClone==sheepDeepClone2);
    }
}
